package com.atguigu.servlet.model;

import com.atguigu.bean.Book;
import com.atguigu.bean.User;
import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
import com.atguigu.service.BookService;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.servlet.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import sun.applet.Main;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class UserServlet extends BaseServlet {
    //user?flag=usernamepanduan
    protected void usernamepanduan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取数据
        String username = request.getParameter("username");
        //2.去数据库查询是否存在该用户名
        UserService userService = new UserServiceImpl();
        User userByUsername = userService.findUserByUsername(username);
        //判断
        String data="no";
        if(userByUsername==null){
            data="yes";
        }
        //3.响应
        response.getWriter().write(data);
    }
        //user?flag=pagesIndex
    protected void pagesIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发
        //request.getRequestDispatcher("index").forward(request,response);
       //重定向  response.sendRedirect("LoginSucceed.html");
       // System.out.println(request.getContextPath());
        //response.sendRedirect(request.getContextPath()+"/index.html");
        //查询books表中的所有的数据
        BookService bookService = new BookServiceImpl();
        List<Book> allBook = bookService.findAllBook();
        request.setAttribute("allBook",allBook);
        this.processTemplate("index",request,response);
    }
    protected void tologin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        this.processTemplate("user/login",request,response);
    }
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取请求
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("password = " + password);
        System.out.println("username = " + username);
        UserService userService = new UserServiceImpl();
        User userlogin = userService.login(username,password);
        if(userlogin!=null){
                request.getSession().setAttribute("userlogin",userlogin);
                this.processTemplate("user/login_success",request,response);
        }else {
            request.setAttribute("test2","用户名或密码错误");
            this.processTemplate("user/login",request,response);
        }

    }
    //user?flag=regist
    protected void toregist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        this.processTemplate("user/regist",request,response);
}
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //注册用户
        //验证码
        //获取验证码
        String code = request.getParameter("code");
        System.out.println("code = " + code);
        //然后在获取正确的验证码
        Object trueCode = request.getSession().getAttribute("trueCode");
        System.out.println("trueCode = " + trueCode);
        //对验证码进行判断
        if(code.equals(trueCode)){
            //1.获取全部的数据
            Map<String, String[]> parameterMap = request.getParameterMap();
            //创建user对象
            User user = new User();
            //将数据放入user对象中
            try {
                BeanUtils.populate(user,parameterMap);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //调用新增方法
            UserService userService = new UserServiceImpl();
            boolean regist = userService.regist(user);
              if(regist){
                  request.setAttribute("username","username");
                this.processTemplate("user/regist_success",request,response);
                  }
            }else{
            request.setAttribute("errormsg","验证码错误");
                this.processTemplate("user/regist",request,response);
                //response.sendRedirect(request.getContextPath()+"/user?falg=tologin");

        }

    }
    //user?flag=logout
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/user?flag=pagesIndex");
    }
}
